package com.wafersystems.springbootdemo.config;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理
 * @author tandk
 * @date 2018/12/13 14:34
 */
@ControllerAdvice
public class GlobalExcepitonHandler {

    /**
     * 全局捕获异常，只要作用在@RequestMapping方法上，均会捕获到
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value=Exception.class)
    public Object errorHandler(Exception ex){
        Map map = new HashMap<String, Object>(2);
        map.put("code", 500);
        map.put("message", ex.getMessage());
        return map;
    }
}
